package cn.house.controller.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.house.common.ApplyStatus;
import cn.house.common.LayuiTableResult;
import cn.house.common.Pagination;
import cn.house.common.PublicResultJosn;
import cn.house.controller.BaseController;
import cn.house.dto.ApplyBaseInfoCheckResult;
import cn.house.entity.ApplyBaseInfo;
import cn.house.service.ApplyBaseInfoService;

/**
 * <p>
 * 申请人申请的基本信息表 前端控制器
 * </p>
 *
 * @author yuyong
 * @since 2019-02-21
 */
@RestController
@RequestMapping("/apply-base-info")
public class ApplyBaseInfoController extends BaseController {

	@Autowired
	ApplyBaseInfoService applyBaseInfoService;

	/**
	 * 分页模糊查询
	 */
	@GetMapping
	public LayuiTableResult selectPage(Pagination pagination, ApplyBaseInfo baseInfo) {
		IPage<ApplyBaseInfo> iPage = new Page<>(pagination.getPage(), pagination.getPageSize());
		Wrapper<ApplyBaseInfo> queryWrapper = new QueryWrapper<ApplyBaseInfo>(baseInfo);
		IPage<ApplyBaseInfo> pageResult = applyBaseInfoService.page(iPage, queryWrapper);
		LayuiTableResult result = new LayuiTableResult(pageResult.getTotal(), pageResult.getRecords());
		return result;
	}

	/**
	 * 根据业务编号查询申请单、身份证等信息查询
	 */
	@GetMapping("/{id}")
	public PublicResultJosn selectBaseInfoById(@PathVariable Integer id) {
		ApplyBaseInfo result = applyBaseInfoService.getById(id);
		PublicResultJosn resultJosn = new PublicResultJosn(result);
		return resultJosn;
	}

	/**
	 * 根据业务编号查询申请人的身份证信息
	 */
	@GetMapping("/card/{id}")
	public PublicResultJosn selectIDCardInfo(@PathVariable String id) {
		return null;
	}

	/**
	 * 申请通过
	 */
	@PostMapping("/yes")
	public PublicResultJosn applyPast(@RequestBody ApplyBaseInfoCheckResult applyBaseInfo) {
		if (null == applyBaseInfo.getId()) {
			throw new IllegalArgumentException("不合法参数");
		}
		applyBaseInfo.setStatus(ApplyStatus.PASSED);
		applyBaseInfoService.updateById(applyBaseInfo);
		return new PublicResultJosn();
	}

	/**
	 * 申请不通过
	 */
	@PostMapping("/no")
	public PublicResultJosn applyNotPast(@RequestBody ApplyBaseInfoCheckResult applyBaseInfo) {
		if (null == applyBaseInfo.getId()) {
			throw new IllegalArgumentException("不合法参数");
		}
		applyBaseInfo.setStatus(ApplyStatus.NOT_PASSED);
		applyBaseInfoService.updateById(applyBaseInfo);
		return new PublicResultJosn();
	}

}
